﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Be;
using WebGui.BllBookingService;
using WebGui.BllLessonService;
using System.Web.UI;

namespace WebGui.Admin {

    /// <summary>
    /// The code-behind class for the New.aspx site
    /// </summary>
    public partial class New : Page {

        /// <summary>
        /// The BookingServiceClient
        /// </summary>
        private BookingServiceClient _booking;

        /// <summary>
        /// The lesson
        /// </summary>
        private BeLesson _event;

        /// <summary>
        /// The LessonServiceClient
        /// </summary>
        private LessonServiceClient _lesson;

        /// <summary>
        /// The latitude of Køretekninsk Anlæg in Varde
        /// </summary>
        private double _ladkoreteknisk = 55.6247;
        
        /// <summary>
        /// The longitude of Køreteknisk Anlæg in Varde
        /// </summary>
        private double _logkoreteknisk = 8.5119;

        /// <summary>
        /// The id of the driving lesson
        /// </summary>
        private int _koretime = 4;
        
        /// <summary>
        /// The id of the theory lesson
        /// </summary>
        private int _teoritime = 2;

        /// <summary>
        /// Loads the page
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            _booking = (BookingServiceClient) Session["Booking"];
            _lesson = (LessonServiceClient) Session["Lesson"];
            if(!IsPostBack) {
                LoadSelecetedinfo();
                DdlType.SelectedIndex = _koretime;
                DdlUser.DataSource = Roles.GetUsersInRole("elev");
                DdlUser.DataBind();
                DdlUser.Visible = false;
                TxtStart.Text = _event.StartTime.ToString();
                TxtEnd.Text = _event.EndTime.ToString();
            }
        }

        /// <summary>
        /// Change the selected index in the dropdownlist
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void DdlType_SelectedIndexChanged(object sender, EventArgs e) {
            if(DdlType.SelectedIndex != _koretime && DdlType.SelectedIndex != _teoritime) {
                DdlUser.Visible = true;
                lblElev.Visible = true;
            } else {
                DdlUser.Visible = false;
                lblElev.Visible = false;
            }
        }

        /// <summary>
        /// Get the selected lesson info
        /// </summary>
        private void LoadSelecetedinfo() {
            try {
                DateTime start = Convert.ToDateTime(Request.QueryString["start"]);
                DateTime end = Convert.ToDateTime(Request.QueryString["end"]);
                _event = new BeLesson(start, end, Membership.GetUser().ProviderUserKey.ToString(), "", 1);
            } catch(Exception ex) {
                Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>");
            }
        }

        /// <summary>
        /// The close button click
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void BtnClose_Click(object sender, EventArgs e) {
            Modal.Close(this);
        }

        /// <summary>
        /// The save button click
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void BtnSave_Click(object sender, EventArgs e) {
            try {
                string str;
                int id = Convert.ToInt32(Request.QueryString["id"]);
                DateTime start = Convert.ToDateTime(TxtStart.Text);
                DateTime end = Convert.ToDateTime(TxtEnd.Text);
                int Type = Convert.ToInt32(DdlType.SelectedValue);
                string comment = TxtComment.Text;
                BeLesson newlesson = new BeLesson(start, end, Membership.GetUser().ProviderUserKey.ToString(), comment, Type);
                if(DdlType.SelectedIndex != _koretime && DdlType.SelectedIndex != _teoritime) {
                    BeLesson lessonid = null;
                    str = _lesson.SaveLesson(newlesson);
                    List<BeLesson> lessons = _lesson.GetLessons().ToList();
                    foreach(var beLesson in lessons) {
                        if(beLesson.StartTime == start) {
                            lessonid = new BeLesson(beLesson.Id);
                        }
                    }
                    BeBooking newbooking = new BeBooking(start, end, _ladkoreteknisk, _logkoreteknisk, _ladkoreteknisk, _logkoreteknisk, Membership.GetUser(DdlUser.SelectedValue).ProviderUserKey.ToString(), Membership.GetUser().ProviderUserKey.ToString(), lessonid.Id);
                    _booking.SaveBooking(newbooking);
                } else {
                    str = _lesson.SaveLesson(newlesson);
                }
                Modal.Close(this, "OK");
            } catch(Exception ex) {
                Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>");
            }
        }
    }
}